import {
  RepairWorkOrderType,
  AddOrUpdateRepairWorkOrderQueryType,
  RepairWorkOrderStatus,
} from "../types/repairWorkOrder";

// Mock数据
const mockRepairWorkOrderList: RepairWorkOrderType[] = [
  {
    id: "1",
    content: "报修工单1",
    deviceId: "设备1",
    status: RepairWorkOrderStatus.IN_PROGRESS,
    startTime: 1716163200000,
    endTime: 1716163200000,
    maintainerCode: "张三",
  },
  {
    id: "2",
    content: "报修工单2",
    deviceId: "设备2",
    status: RepairWorkOrderStatus.IN_PROGRESS,
    startTime: 1716163200000,
    endTime: 1716163200000,
    maintainerCode: "李四",
  },
  {
    id: "3",
    content: "报修工单3",
    deviceId: "设备3",
    status: RepairWorkOrderStatus.IN_PROGRESS,
    startTime: 1716163200000,
    endTime: 1716163200000,
    maintainerCode: "王五",
  },
  {
    id: "4",
    content: "报修工单4",
    deviceId: "设备4",
    status: RepairWorkOrderStatus.IN_PROGRESS,
    startTime: 1716163200000,
    endTime: 1716163200000,
    maintainerCode: "赵六",
  },
  {
    id: "5",
    content: "报修工单5",
    deviceId: "设备5",
    status: RepairWorkOrderStatus.IN_PROGRESS,
    startTime: 1716163200000,
    endTime: 1716163200000,
    maintainerCode: "孙七",
  },
];

// 生成唯一ID
const generateId = (): string => {
  return Date.now().toString() + Math.random().toString(36).substr(2, 9);
};

// 模拟延迟
const delay = (ms = 300): Promise<void> => {
  return new Promise((resolve) => setTimeout(resolve, ms));
};

// 获取报修工单列表的mock
export const mockGetRepairWorkOrderList = async (): Promise<{
  status: number;
  data: RepairWorkOrderType[];
  message: string;
}> => {
  await delay();

  return {
    status: 0,
    data: mockRepairWorkOrderList,
    message: "获取报修工单列表成功",
  };
};

// 添加或更新报修工单的mock
export const mockAddOrUpdateRepairWorkOrder = async (
  params: AddOrUpdateRepairWorkOrderQueryType
): Promise<{
  status: number;
  data: RepairWorkOrderType | null;
  message: string;
}> => {
  await delay();

  try {
    // 编辑报修工单
    if (params.id) {
      const index = mockRepairWorkOrderList.findIndex(
        (repairWorkOrder) => repairWorkOrder.id === params.id
      );

      if (index === -1) {
        return {
          status: 1,
          data: null,
          message: "报修工单不存在",
        };
      }

      // 更新报修工单信息
      const updatedRepairWorkOrder: RepairWorkOrderType = {
        ...mockRepairWorkOrderList[index],
        ...Object.fromEntries(
          Object.entries(params).filter(
            ([_, value]) => value !== null && value !== undefined
          )
        ),
      } as RepairWorkOrderType;

      mockRepairWorkOrderList[index] = updatedRepairWorkOrder;

      return {
        status: 0,
        data: updatedRepairWorkOrder,
        message: "更新报修工单成功",
      };
    }
    // 添加新报修工单
    else {
      // 验证必填字段
      if (!params.content || !params.startTime) {
        return {
          status: 1,
          data: null,
          message: "报修工单名称和报修工单开始日期和报修工单结束日期为必填项",
        };
      }

      // 检查报修工单名称是否已存在
      const existingRepairWorkOrder = mockRepairWorkOrderList.find(
        (repairWorkOrder) => repairWorkOrder.content === params.content
      );
      if (existingRepairWorkOrder) {
        return {
          status: 1,
          data: null,
          message: "该报修工单名称已存在",
        };
      }

      const newRepairWorkOrder: RepairWorkOrderType = {
        id: generateId(),
        content: params.content || "",
        deviceId: params.deviceId || "",
        status: params.status as RepairWorkOrderStatus,
        startTime: params.startTime || null,
        maintainerCode: params.maintainerCode || "",
      };

      mockRepairWorkOrderList.unshift(newRepairWorkOrder); // 添加到列表开头

      return {
        status: 0,
        data: newRepairWorkOrder,
        message: "添加报修工单成功",
      };
    }
  } catch (error) {
    return {
      status: 1,
      data: null,
      message: "操作失败，请重试",
    };
  }
};

// 删除报修工单的mock（可选，如果需要的话）
export const mockDeleteRepairWorkOrder = async (
  id: string
): Promise<{ status: number; data: null; message: string }> => {
  await delay();

  const index = mockRepairWorkOrderList.findIndex(
    (repairWorkOrder) => repairWorkOrder.id === id
  );

  if (index === -1) {
    return {
      status: 1,
      data: null,
      message: "报修工单不存在",
    };
  }

  mockRepairWorkOrderList.splice(index, 1);

  return {
    status: 0,
    data: null,
    message: "删除报修工单成功",
  };
};

// 根据ID获取单个报修工单的mock（可选）
export const mockGetRepairWorkOrderById = async (
  id: string
): Promise<{
  status: number;
  data: RepairWorkOrderType | null;
  message: string;
}> => {
  await delay();

  const repairWorkOrder = mockRepairWorkOrderList.find(
    (repairWorkOrder) => repairWorkOrder.id === id
  );

  if (!repairWorkOrder) {
    return {
      status: 1,
      data: null,
      message: "报修工单不存在",
    };
  }

  return {
    status: 0,
    data: repairWorkOrder,
    message: "获取报修工单详情成功",
  };
};
